package com.wushi.lenovo.asia5b.app_di.other.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.app_di.other.mvpview.CodeMvpView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.profile.CodeModel;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.CHANGE_LOGINPASS;
import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.CHANGE_MOBILE;
import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.CHANGE_PAYMENTPASS;
import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.REGISTER;
import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.RESET_LOGINPASS;
import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.RESET_PAYPASS;
import static com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter.CodeType.SMS_LOGIN;

/**
 * Created by zhangyuncai on 2018/12/7.
 */
public class CodePresenter<T extends CodeMvpView> extends BasePresenter<T> {

    public ApiService apiService;
    private Disposable intervalDisposable;

    public enum CodeType {
        REGISTER,
        CHANGE_MOBILE,
        CHANGE_LOGINPASS,
        CHANGE_PAYMENTPASS,
        RESET_LOGINPASS,
        RESET_PAYPASS,
        SMS_LOGIN
    }

    public CodePresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void sendCode(String mobile_num, String calling_code, CodeType codeType) {
        Observable<ResultModel<CodeModel>> observable = null;
        if (codeType == CHANGE_MOBILE) {
            observable = apiService.sendCodeForChangeMobile(mobile_num, calling_code);
        } else if (codeType == REGISTER) {
            observable = apiService.sendCodeForRegister(mobile_num, calling_code);
        } else if (codeType == SMS_LOGIN) {
            observable = apiService.sendCodeForSmsLogin(mobile_num, calling_code);
        }
        if (observable == null) {
            return;
        }

        baseSubscribe(observable
                , resultModel -> {
                    getView().sendCodeResult(resultModel, codeType);
                });
    }

    public void sendCode(CodeType codeType) {
        Observable<ResultModel<CodeModel>> observable = null;
        if (codeType == CHANGE_LOGINPASS) {
            observable = apiService.sendCodeForChangeLoginPass(UserInfo.getMemberId() + "");
        } else if (codeType == CHANGE_PAYMENTPASS) {
            observable = apiService.sendCodeForChangePaymentPass(UserInfo.getMemberId() + "");
        } else if (codeType == RESET_PAYPASS) {
            observable = apiService.sendCodeForResetPayPass(UserInfo.getMemberId() + "");
        }

        if (observable == null) {
            return;
        }

        baseSubscribe(observable
                , resultModel -> {
                    getView().sendCodeResult(resultModel, codeType);
                });
    }

    public void sendCode(String email, CodeType codeType) {

        Observable<ResultModel<CodeModel>> observable = null;
        if (codeType == RESET_LOGINPASS) {
            observable = apiService.sendCodeForResetLoginPass(email);
        }

        if (observable == null) {
            return;
        }

        baseSubscribe(observable
                , resultModel -> {
                    getView().sendCodeResult(resultModel, codeType);
                });
    }


    public void interval() {
        if (intervalDisposable != null) {
            composite.remove(intervalDisposable);
            intervalDisposable.dispose();
        }
        intervalDisposable = Observable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        getView().intervalResult(59 - aLong);
                    }
                }, throwable -> {
                    getView().intervalResult(0);
                });
        composite.add(intervalDisposable);
    }


}